//
//  ViewExtension.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/9.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
extension UIView{
    func showhubInView(inView:UIView,mode:MBProgressHUDMode,tip:String?,whenDown:MBProgressHUDCompletionBlock?)->MBProgressHUD{
        let hub = MBProgressHUD.showHUDAddedTo(inView, animated: true)
        hub.completionBlock = whenDown
        if self is UIWindow{
            hub.yOffset += 32.0
        }
        hub.removeFromSuperViewOnHide = true
        hub.mode = mode
        hub.margin = 10.0
        hub.labelFont = UIFont.systemFontOfSize(14)
        hub.labelText = tip
        return hub
    }
    func showLoadingTip(tip:String){
        let hub = self.showhubInView(self,mode: MBProgressHUDMode.Indeterminate, tip:tip,whenDown:nil)
        hub.show(true)
    }
    func showLoadingNoBlock(){
        let hub = self.showhubInView(self,mode: MBProgressHUDMode.Indeterminate, tip:nil,whenDown:nil)
        hub.userInteractionEnabled = false
        hub.show(true)
    }
    func showFlashTips(tip:String){
        let hub = self.showhubInView(self,mode: MBProgressHUDMode.Text, tip:tip,whenDown:nil)
        hub.hide(true, afterDelay: 1.5)
        
    }
    func showTips(tip:String,whenDown:MBProgressHUDCompletionBlock){
        let hub = self.showhubInView(self,mode: MBProgressHUDMode.Text, tip:tip,whenDown:whenDown)
        hub.hide(true, afterDelay: 1.5)
    }
    func hideHub(){
        let hub:MBProgressHUD? = MBProgressHUD(forView:self)
        hub?.hidden = true
        hub?.removeFromSuperview()

        var awindow:UIView?
        if self.window != nil{
            awindow = self.window!
        }
        else{
            awindow = ((UIApplication.sharedApplication().delegate?.window)!)!
        }
        let ahub:MBProgressHUD? = MBProgressHUD(forView:awindow)
        ahub?.hidden = true
        ahub?.removeFromSuperview()
    }
}
class LineView: UIView {
    var lineView:UIView!
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor.whiteColor()
        lineView = UIView()
        lineView.backgroundColor = UIColor(red: 234/255.0, green: 234/255.0, blue: 234/255.0, alpha: 1.0)
        self.translatesAutoresizingMaskIntoConstraints = false
        lineView.translatesAutoresizingMaskIntoConstraints = false
    }
    override init(frame:CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        lineView = UIView()
        lineView.backgroundColor = UIColor(red: 234/255.0, green: 234/255.0, blue: 234/255.0, alpha: 1.0)
        self.translatesAutoresizingMaskIntoConstraints = false
        lineView.translatesAutoresizingMaskIntoConstraints = false
    }
    override func layoutSubviews() {
        lineView.frame = CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1)
        self.addSubview(lineView)
        super.layoutSubviews()

    }
}
enum ColorViewStyle{
    case nomal,selected
}
class ColorView: UIView {
    var shapeLayer:CAShapeLayer!
    var gradientLayer:CAGradientLayer!
    func setStyle(style:ColorViewStyle)->Void{
         let path = UIBezierPath()
//        path.moveToPoint(CGPointMake(shapeLayer.bounds.size.width/2.0, shapeLayer.bounds.size.height/2.0))
        switch style{
        case .nomal:
            path.addArcWithCenter(CGPointMake(shapeLayer.bounds.size.width/2.0, shapeLayer.bounds.size.height/2.0), radius:4, startAngle: 0, endAngle:CGFloat(M_PI*2), clockwise: true)
            gradientLayer.colors = [UIColor.clearColor()]
            gradientLayer.startPoint = CGPointZero
            gradientLayer.endPoint = CGPointZero
        case .selected:
            path.addArcWithCenter(CGPointMake(shapeLayer.bounds.size.width/2.0, shapeLayer.bounds.size.height/2.0), radius:7, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
            gradientLayer.colors = [UIColor.yellowColor(),UIColor.orangeColor()]
            gradientLayer.startPoint = CGPointMake(0, 0)
            gradientLayer.endPoint = CGPointMake(1, 1)
        default:break
        }
        shapeLayer.path = path.CGPath
//        gradientLayer.mask = shapeLayer
//        layer.addSublayer(gradientLayer)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 3.0
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.lightGrayColor().CGColor
        let pathRect = CGRectInset(bounds, 4, 4)
        shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.greenColor().CGColor
        shapeLayer.frame = pathRect
        shapeLayer.strokeColor = UIColor.whiteColor().CGColor
        shapeLayer.lineWidth = 2
//        shapeLayer.opacity = 0.5
        layer.addSublayer(shapeLayer)
        
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = pathRect
        
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        
    }
}
class DotView:UIView{
    var shapeLayer:CAShapeLayer!
    var cutNumber:NSInteger = 150
    override init(frame: CGRect) {
        super.init(frame: frame)
        let pathRect = CGRectMake(0, frame.size.height-1, frame.size.width, 1)
        shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.frame = pathRect
        shapeLayer.strokeColor = UIColor.lightGrayColor().CGColor
        shapeLayer.lineWidth = onePix
        //        shapeLayer.opacity = 0.5
        layer.addSublayer(shapeLayer)
        
    }
    func storkeDot()->Void{
        let pathRect = CGRectMake(0, 0,bounds.size.width, 1)
        shapeLayer.frame = pathRect
        let path = CGPathCreateMutable();
        let offset:CGFloat = pathRect.size.width*CGFloat(2)/CGFloat(cutNumber);
        for var number:NSInteger = 0;number < cutNumber/2;number+=2 {
            CGPathMoveToPoint(path, nil, CGFloat(number)*offset, 0)
            CGPathAddLineToPoint(path, nil, CGFloat(number)*offset+offset,0)
        }
        shapeLayer.path = path
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.strokeColor = UIColor.lightGrayColor().CGColor
        shapeLayer.lineWidth = onePix
         shapeLayer.lineJoin = kCALineCapRound;
        //        shapeLayer.opacity = 0.5

        layer.addSublayer(shapeLayer)
        
    }
    override func awakeFromNib() {
        super.awakeFromNib()

    }
}